import React from 'react';
import { Icon } from 'antd';
import intl from 'utils/intl';

function getRegExpStatus(flagCount, regStr, value) {
  var regStrArr = [];

  for (var i = 0; i < flagCount; i++) {
    regStrArr.push(regStr);
  }

  var reg = new RegExp("".concat(regStrArr.join('')));
  return reg.test(value);
}

function renderMsgContent(msg, flag) {
  return /*#__PURE__*/React.createElement(React.Fragment, null, flag ? /*#__PURE__*/React.createElement(Icon, {
    type: "check-circle-o",
    style: {
      color: '#52c41a'
    }
  }) : /*#__PURE__*/React.createElement(Icon, {
    type: "close-circle-o",
    style: {
      color: '#f5222d'
    }
  }), /*#__PURE__*/React.createElement("span", {
    style: {
      marginLeft: 2,
      color: flag ? '#52c41a' : '#f5222d'
    }
  }, msg));
}

export function validatePasswordRule() {
  var value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  var callback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function (e) {
    return e;
  };
  var validData = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  var loginName = validData.loginName,
      digitsCount = validData.digitsCount,
      lowercaseCount = validData.lowercaseCount,
      maxLength = validData.maxLength,
      minLength = validData.minLength,
      notUsername = validData.notUsername,
      specialCharCount = validData.specialCharCount,
      uppercaseCount = validData.uppercaseCount;
  var msg = [];
  var allFlag = false;

  if (minLength && maxLength) {
    var flag = value.length < minLength || value.length > maxLength;
    allFlag = !flag;
    msg.push(renderMsgContent(intl.get('cpaas.common.validation.passwordLength', {
      min: minLength,
      max: maxLength
    }).d("".concat(minLength, "-").concat(maxLength, "\u4E2A\u5B57\u7B26")), !flag));
  }

  if (!notUsername) {
    var _flag = value === loginName;

    allFlag = !_flag && allFlag;

    if (_flag) {
      msg.push(renderMsgContent(intl.get('cpaas.common.validation.notUsername').d("\u5BC6\u7801\u4E0D\u80FD\u4E0E\u767B\u5F55\u540D\u76F8\u540C"), false));
    }
  }

  if (lowercaseCount) {
    var _flag2 = getRegExpStatus(lowercaseCount, '([a-z].*)', value);

    allFlag = _flag2 && allFlag;
    msg.push(renderMsgContent(intl.get('cpaas.common.validation.lowercaseCount', {
      lower: lowercaseCount
    }).d("\u81F3\u5C11\u5305\u542B".concat(lowercaseCount, "\u4E2A\u5C0F\u5199\u5B57\u7B26")), _flag2));
  }

  if (uppercaseCount) {
    var _flag3 = getRegExpStatus(uppercaseCount, '([A-Z].*)', value);

    allFlag = _flag3 && allFlag;
    msg.push(renderMsgContent(intl.get('cpaas.common.validation.uppercaseCount', {
      upper: uppercaseCount
    }).d("\u81F3\u5C11\u5305\u542B".concat(uppercaseCount, "\u4E2A\u5927\u5199\u5B57\u7B26")), _flag3));
  }

  if (digitsCount) {
    var _flag4 = getRegExpStatus(digitsCount, '([0-9].*)', value);

    allFlag = _flag4 && allFlag;
    msg.push(renderMsgContent(intl.get('cpaas.common.validation.digits.min', {
      digitsCount: digitsCount
    }).d("\u81F3\u5C11\u5305\u542B".concat(digitsCount, "\u4E2A\u6570\u5B57")), _flag4));
  }

  if (specialCharCount) {
    var _flag5 = getRegExpStatus(specialCharCount, '([~`@#$%^&*/\\-_=+|/()<>,.;:!].*)', value);

    allFlag = _flag5 && allFlag;
    msg.push(renderMsgContent(intl.get('cpaas.common.validation.specialChart.min', {
      specialCount: specialCharCount
    }).d("\u81F3\u5C11\u5305\u542B".concat(specialCharCount, "\u4E2A\u7279\u6B8A\u5B57\u7B26 ([~`@#$%^&*/\\-_=+|/()<>,.;:!].*)")), _flag5));
  }

  if (value) {
    if (msg.length > 0) {
      var msgDom = msg.map(function (item) {
        return /*#__PURE__*/React.createElement("div", {
          key: item
        }, item);
      });
      callback(allFlag ? undefined : msgDom);
    } else {
      callback();
    }
  } else {
    callback();
  }
}